package cn.zhonghuanzhiyuan.sprc.common.mock;

import cn.org.bachelor.iam.IamContext;
import cn.org.bachelor.iam.vo.UserVo;
import cn.org.bachelor.web.util.RequestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 访问身份识别拦截器
 * <p>
 * <b>NOTE:</b> 根据已经配置的服务和功能授权信息，控制用户的访问权限
 *
 * @author liuzhuo
 * @version 1.0
 */

public class UserMockInterceptor extends HandlerInterceptorAdapter {
    private static final Logger logger = LoggerFactory.getLogger(UserMockInterceptor.class);
    @Autowired
    private IamContext iamContext;
    @Resource
    private JwtMockConfig jwtMockConfig;


    //private Set<String> urlCache;
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        logger.info("进入用户信息拦截器，开始组装用户信息：" + request.getServletPath());

        UserVo user = new UserVo();

        user.setId(jwtMockConfig.getId());
        user.setAccessToken(jwtMockConfig.getAccessToken());
        user.setOrgId(jwtMockConfig.getOrgId());
        user.setName(jwtMockConfig.getUserName());
        user.setOrgName(jwtMockConfig.getOrgName());
        user.setDeptId(jwtMockConfig.getDeptId());
        user.setDeptName(jwtMockConfig.getDeptName());

        // iamContext.setUser(user);
        iamContext.setLogonUser(user);
//        iamSysService.refreshToken(request, response, user);
        iamContext.setRemoteIP(RequestUtil.getIpAddr(request));
        return true;
    }
}
